﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Resources;
using Cesave.Domain.Core;
using System.Data;

namespace Cesave.Domain.PermisosModule.Contracts.RiegoSistemas
{
	public class RiegoSistemaService
		: IRiegoSistemaService
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private IRiegoSistemaRepository _riegoSistemaRepository;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="riegoSistemaRepository"></param>
		public RiegoSistemaService(IRiegoSistemaRepository riegoSistemaRepository)
		{
			if (riegoSistemaRepository == null)
				throw new ArgumentNullException(Messages.exception_DependenciesAreNotInitialized, "riegoSistemaRepository");
			_riegoSistemaRepository = riegoSistemaRepository;
		}

		#endregion

		#region IRiegoSistemaService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="riegoSistema"></param>
		public void AddRiegoSistema(Entities.RiegoSistema riegoSistema)
		{
			if (riegoSistema == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "riegoSistema");
			IUnitOfWork unitOfWork = _riegoSistemaRepository.StoreContext;
			_riegoSistemaRepository.Add(riegoSistema);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="riegoSistema"></param>
		public void ChangeRiegoSistema(Entities.RiegoSistema riegoSistema)
		{
			if (riegoSistema == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "riegoSistema");
			IUnitOfWork unitOfWork = _riegoSistemaRepository.StoreContext;
			_riegoSistemaRepository.Modify(riegoSistema);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public List<Entities.RiegoSistema> FindAllRiegoSistemas()
		{
			return _riegoSistemaRepository.GetAll().OrderBy(r => r.Orden).ToList();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Entities.RiegoSistema> FindRiegoSistemasByStatus(bool status = true)
		{
			RiegoSistemasByStatusSpecification specification = new RiegoSistemasByStatusSpecification(status);
			return _riegoSistemaRepository.FindRiegoSistemasByStatus(specification).ToList();
		}

		#endregion

		#region IDisposable Members

		/// <summary>
		/// 
		/// </summary>
		public void Dispose()
		{
			if (_riegoSistemaRepository != null && _riegoSistemaRepository.StoreContext != null)
				_riegoSistemaRepository.StoreContext.Dispose();
		}

		#endregion
	}
}
